import { connector, createApp, Session } from "mydog";
import { serverInit } from "./app/appConfigure";
import { getEncodeDecodeFunc } from "./app/encode_decode";
import { onClientDisconnect } from "./servers/connector/handler/main";

const app = createApp();

app.appName = "game server"
app.setConfig("connector", { "connector": connector.Tcp, heartbeat: 10, interval: 30, clientOnCb: clientOnCb, clientOffCb: clientOffCb });
app.setConfig("encodeDecode", getEncodeDecodeFunc());
app.setConfig("rpc", { "interval": 30 });
app.setConfig("logger", logger);

app.configure("all", function () {
  serverInit(app);
});

app.start();

process.on("uncaughtException", function (err: any) {
  console.error(`uncaughtException Caught exception: ${err}`)
});

process.on('unhandledRejection', (reason, promise) => {
  console.log(`Unhandled Rejection at: ${promise} reason: ${reason}`);
});

function clientOnCb(session: Session) {
  console.log("one client on");
}

function clientOffCb(session: Session) {
  console.log(`one client off ${session.uid}`);
  onClientDisconnect(session);
}

function logger(level: string, msg: string) {
  //console.log(`[${level}]: ${msg}`);
  if (level === "warn" || level === "error") {
    console.log(`[${level}]-[${app.serverId}]: ${msg}`);
  }
}
